<?php

namespace Admin\Controller;

use Think\Controller;

class GoodsController extends CommonController
{

    public function ajaxGoods()//查询 lst页面
    {
        $id = session('id');

        $model_goods = D('goods');
        $l = I('get.limit');
        $l = !$l ? 6 : $l;

        $p = I('get.page');
        $p = $p <= 0 ? 1 : $p;

        $p = ($p - 1) * $l;//(页码 - 1 ) * 单页条数

        $sql = sprintf("SELECT * FROM goods WHERE goods_sort = '%s'  limit $p,$l", $id );
        $res = $model_goods->query($sql);// 从第$p条开始 取$n条

        $count = $model_goods->query("SELECT count(1)  AS num FROM goods ");

//        $this->ajaxReturn(array('data' => $res, 'count' => $count[0]['num'], 'code' => empty($res) ? 1 : 0, 'msg' => empty($res) ? '暂无信息' : '提示信息'));

        $this->ajaxRet(self::RET_SUCCESS, "暂无信息", $res, $count[0]['num']);

    }


    public function file()//上传文件
    {
        if ($_FILES['file']['tmp_name'] == '') {
            $this->ajaxRet(self::RET_FAILED, "上传失败");
        }

        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'image', 'JPEG');// 设置附件上传类型
        $upload->rootPath = $_SERVER['DOCUMENT_ROOT']; // 设置附件上传根目录
        $upload->savePath = '/Public/Uploads/'; // 设置附件上传（子）目录
        // 上传单个文件
        $info = $upload->uploadOne($_FILES['file']);

        if (!$info) {
            $this->ajaxRet(self::RET_FAILED, $upload->getError());
        }

        $fileName = $info['savepath'] . $info['savename'];// 上传成功 获取上传文件信息

        $this->ajaxRet(self::RET_SUCCESS, "上传成功", ["fileName" => $fileName]);
    }

    public function add()//添加
    {
        $model_goods = D('goods');
        if (IS_POST) {
            $data['goods_title'] = I('post.goods_title');
            $data['goods_sort'] = I('post.goods_sort');
            $data['goods_pic'] = I('post.goods_pic');
            $data['goods_desc'] = I('post.goods_desc');
            $data['goods_price'] = I('post.goods_price');
            if ($model_goods->create($data)) {
                if ($model_goods->add()) {
                    $this->success('添加商品成功！', U('Goods/lst'));
                } else {
                    $this->arror('添加商品失败！');
                }
            } else {
                $this->error($model_goods->getError());
            }

            return;
        }


        $this->display();
    }

    public function del()//删除
    {
        $POST = $_POST;
        $model_del = D('goods');
        $model_del->where("id=$POST[id]")->delete();
        $count = $model_del->query("SELECT count(1)  as num FROM link ");
//        $this->ajaxReturn(array('data' => $del, 'count' => $count[0]['num'], 'code' => empty($del) ? 1 : 0, 'msg' => empty($del) ? '暂无信息' : '提示信息'));
        $this->ajaxRet(self::RET_SUCCESS, "删除成功", $model_del, $count[0]['unm']);

    }

    public function up()//更新数据
    {
        $post = $_POST;
        $post['id'];
        $model_up = D('goods');
        $model_up->where("id=$post[id]")->save($post);
    }

}